import numpy as np
import torch
from torchvision import utils as vutils
import cv2 as cv
import os
def save_image_tensor(input_tensor: torch.Tensor, filename):
    """
    将tensor保存为图片
    :param input_tensor: 要保存的tensor
    :param filename: 保存的文件名
    """
    assert (len(input_tensor.shape) == 4 and input_tensor.shape[0] == 1)
    # 复制一份
    input_tensor = input_tensor.clone().detach()
    # 到cpu
    input_tensor = input_tensor.to(torch.device('cpu'))
    # 反归一化
    # input_tensor = unnormalize(input_tensor)
    vutils.save_image(input_tensor, filename)
def play_video_sequence(imagepath,speed):
    img = cv.imread(imagepath)
    path_list = os.listdir(imagepath)
    path_list.sort(key=lambda x: int(x[:-4]))
    print(path_list)
    for i in range(len(path_list)):
        img = cv.imread(imagepath + path_list[i])
        cv.imshow('window_title', img)
        cv.waitKey(speed)
def play_muti_video(imagepath,speed):
    path_list = os.listdir(imagepath)
    path_list.sort(key=lambda x: int(x[:-4]))
    print(path_list)
    for i in range(len(path_list)):
        img = cv.imread(imagepath + path_list[i])
        img2 = cv.imread(imagepath + path_list[i+5])
        print(img)
        imgs = np.hstack([img,img2])
        # 展示多个
        cv.imshow("mutil_pic", imgs)
        #等待关闭
        cv.waitKey(speed)
if __name__ == '__main__':
    imagepath = "./bedroom/image/"
    # play_video_sequence(imagepath=imagepath,speed=10)
    play_muti_video(imagepath=imagepath, speed=10)
